﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessObject;
using YamahaTuanTruc.BusinessService;
using Qios.DevSuite.Components;

namespace YamahaTuanTruc.Controls
{
    public partial class QpMaintenanceForm : QCustomPanel
    {
        public bool IsCreateNew { get; set; }
        public SuaChua maintenance { get; set; }

        public void OnLoad()
        {
            using (var service = new BusinessServiceClient())
            {
                Xe[] lst = service.GetBikeList();
                foreach (var tmp in lst)
                {
                    qcibBienSo.Items.Add(tmp.ID);
                }
            }
        }
        public QpMaintenanceForm()
        {
            maintenance = new SuaChua();
            InitializeComponent();
            IsCreateNew = true;
            //OnLoad();

        }

        public void InitializeState()
        {
            if (IsCreateNew)
            {
                qcibNgay.InputBox.Clear();
                qcibKhachHang.InputBox.Clear();
                qcibBienSo.SelectedValue = null;
                qcibDienThoai.InputBox.Clear();
                qcibLoaiXe.InputBox.Clear();
                qcibNVKT.InputBox.Clear();
                qcibChiTietCongViec.InputBox.Clear();
                qcibChiPhiPhatSinh.InputBox.Clear();
                qcibSoLuong.InputBox.Clear();
                qcibChiPhi.InputBox.Clear();
                qcibDonGia.InputBox.Clear();
                qcibTBD.InputBox.Clear();
                IsCreateNew = false;
            }
            else
            {
                maintenance.Xe.NgayNhanXe = Convert.ToDateTime(qcibNgay.InputBox.Text);
               // maintenance.Xe.KhachHang.Ten = qcibKhachHang.InputBox.Text;//modify by data source
                maintenance.ID = long.Parse(qcibBienSo.SelectedValue.ToString());
               // maintenance.Xe.KhachHang.DienThoai = qcibDienThoai.InputBox.Text;//
                maintenance.Xe.Ten = qcibLoaiXe.InputBox.Text;
                //qcibNVKT.InputBox.Text = maintenance.Xe.NhanVien.Ten; modify by data source
                maintenance.ChiTietCongViec = qcibChiTietCongViec.InputBox.Text;
                maintenance.CongViecPhatSinh = qcibChiPhiPhatSinh.InputBox.Text;
                maintenance.SoLuong = int.Parse(qcibSoLuong.InputBox.Text);
                maintenance.ChiPhiPhatSinh = int.Parse(qcibChiPhi.InputBox.Text);
                maintenance.DonGia = int.Parse(qcibDonGia.InputBox.Text);
                maintenance.TienBaoDuong = int.Parse(qcibTBD.InputBox.Text);
            }
            qcbCapNhat.Enabled = !IsCreateNew;
            qcbTaoMoi.Enabled = !IsCreateNew;
            qcbThem.Enabled = IsCreateNew;
            qcbXoa.Enabled = !IsCreateNew;
        }

        private void qcbTaoMoi_ItemActivated(object sender, QCompositeEventArgs e)
        {
            maintenance = null;
            InitializeState();
        }

        private void qCompositeControl1_ItemActivated(object sender, QCompositeEventArgs e)
        {
            
        }

        private void qcbCapNhat_ItemActivated(object sender, QCompositeEventArgs e)
        {
            InitializeState();
            using (var service = new BusinessServiceClient())
            {
                bool res = service.UpdateMaintenance(maintenance);
                if (res)
                {
                    MessageBox.Show("Cập nhập thông tin sửa chữa thành công.");
                    OnDataUpdated(this, new EventArgs());
                }
                else
                {
                    MessageBox.Show("Cập nhật thông tin sửa chữa không thành công, vui lòng kiểm tra lại thông tin", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void qcbXoa_ItemActivated(object sender, QCompositeEventArgs e)
        {
            if (MessageBox.Show("Xóa khách hàng này?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                InitializeState();
                using (var service = new BusinessServiceClient())
                {
                    bool res = service.DeleteMaintenance(maintenance);
                    if (res)
                    {
                        OnDataUpdated(this, new EventArgs());
                    }
                    else
                    {
                        MessageBox.Show("Có lỗi xảy ra khi xóa. Vui lòng kiểm tra lại thông tin.");
                    }
                }
            }
        }

        private void qcbThem_ItemActivated(object sender, QCompositeEventArgs e)
        {
            using (var service = new BusinessServiceClient())
            {
                InitializeState();
                bool res = service.AddMaintenance(maintenance);
                if (res)
                {
                    MessageBox.Show("Thêm sửa chữa thành công.");
                    OnDataUpdated(this, new EventArgs());
                }
                else
                {
                    MessageBox.Show("Thêm sửa chữa thất bại.");
                }

            }
        }

        private void qcibBienSo_SelectedItemChanged(object sender, EventArgs e)
        {
            string id = qcibBienSo.SelectedItem.ToString();
            using (var service = new BusinessServiceClient())
            {
                Xe x = service.GetBikeByID(id);
                if (x != null)
                {
                    qcibKhachHang.InputBox.Text = x.KhachHang.Ten;
                    qcibNVKT.InputBox.Text = x.NhanVien.Ten;
                    qcibDienThoai.InputBox.Text = x.KhachHang.DienThoai;
                    qcibLoaiXe.InputBox.Text = x.Ten;
                }
            }
        }


    }
}